package com.zmc.lostfound.post.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmc.lostfound.common.exception.MyException;
import com.zmc.lostfound.common.utils.PageParams;
import com.zmc.lostfound.common.utils.PageResult;
import com.zmc.lostfound.common.utils.Result;
import com.zmc.lostfound.post.dao.PostImageDao;
import com.zmc.lostfound.post.entity.PostImageEntity;
import com.zmc.lostfound.post.service.PostImageService;
import com.zmc.lostfound.post.vo.PostImageSaveVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ZMC, GXC
 * @description 帖子图片信息管理业务实现
 * @email 2869581855@qq.com
 * @date 2023-07-05 15:49:44
 */
@Service
public class PostImageServiceImpl extends ServiceImpl<PostImageDao, PostImageEntity> implements PostImageService {

    /**
     * 分页查询帖子图片信息
     *
     * @param pageParams 分页参数
     * @return PageResult
     */
    @Override
    public PageResult queryPage(PageParams pageParams) {
        Integer pageNo = pageParams.getPageNo();
        Integer pageSize = pageParams.getPageSize();
        Page<PostImageEntity> page = new Page<>(pageNo, pageSize);
        QueryWrapper<PostImageEntity> queryWrapper = new QueryWrapper<>();
        return new PageResult(this.page(page, queryWrapper));
    }

    /**
     * 按帖子id查询全部图片信息
     *
     * @param postId 帖子id
     * @return List<PostImageEntity>
     */
    @Override
    public List<PostImageEntity> queryInfoByPostId(Long postId) {
        if (postId == null || postId <= 0) {
            MyException.throwException("查询失败: 帖子id不能为null且必须大于0");
        }
        return this.query().eq("post_id", postId).list();
    }

    /**
     * 按id查询帖子图片信息
     *
     * @param id 帖子图片id
     * @return PostImageEntity
     */
    @Override
    public PostImageEntity queryInfoById(Long id) {
        if (id == null || id <= 0) {
            MyException.throwException("查询失败: 帖子图片id不能为null且必须大于0");
        }
        return this.getById(id);
    }

    /**
     * 新增帖子图片信息
     *
     * @param postImageSaveVO 帖子图片信息新增/修改请求体
     * @return Result
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result insertPostImage(PostImageSaveVO postImageSaveVO) {
        PostImageEntity postImageEntity = BeanUtil.copyProperties(postImageSaveVO, PostImageEntity.class);
        if (!this.save(postImageEntity)) {
            MyException.throwException("新增帖子图片信息失败");
        }
        return Result.ok("新增成功");
    }

    /**
     * 批量新增帖子图片信息
     *
     * @param postId     帖子id
     * @param imgUrlList 帖子图片信息
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertBatchWithPostId(Long postId, List<String> imgUrlList) {
        if (CollectionUtil.isEmpty(imgUrlList)) {
            return;
        }
        List<PostImageEntity> postImageList = imgUrlList.stream().map(imgUrl -> {
            PostImageEntity postImageEntity = new PostImageEntity();
            postImageEntity.setPostId(postId);
            postImageEntity.setImgUrl(imgUrl);
            return postImageEntity;
        }).collect(Collectors.toList());
        if (!this.saveBatch(postImageList)) {
            MyException.throwException("批量新增帖子图片信息失败");
        }
    }

    /**
     * 修改帖子图片信息
     *
     * @param postImageSaveVO 帖子图片信息新增/修改请求体
     * @return Result
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result updatePostImage(PostImageSaveVO postImageSaveVO) {
        PostImageEntity postImageEntity = BeanUtil.copyProperties(postImageSaveVO, PostImageEntity.class);
        if (!this.updateById(postImageEntity)) {
            MyException.throwException("修改帖子图片信息失败");
        }
        return Result.ok("修改成功");
    }

    /**
     * 批量修改帖子的图片信息
     *
     * @param postId     帖子id
     * @param imgUrlList 图片信息
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateBatchByPostId(Long postId, List<String> imgUrlList) {
        if (CollectionUtil.isNotEmpty(imgUrlList)) {
            // 1.按帖子id删除帖子的所有图片信息
            this.deleteBatchByPostId(postId);
            // 2.批量新增帖子图片信息
            this.insertBatchWithPostId(postId, imgUrlList);
        }
    }

    /**
     * 批量删除
     *
     * @param postImageIds 帖子图片ids
     * @return Result
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result deleteBatchByIds(List<Long> postImageIds) {
        if (CollectionUtil.isEmpty(postImageIds)) {
            MyException.throwException("删除失败: 帖子图片ids不能为空");
        }
        this.removeByIds(postImageIds);
        return Result.ok("删除成功");
    }

    /**
     * 按帖子id批量删除图片信息
     *
     * @param postId 帖子di
     * @return Result
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result deleteBatchByPostId(Long postId) {
        if (postId == null || postId <= 0) {
            MyException.throwException("删除失败: 帖子id不能为null且必须大于0");
        }
        this.remove(new QueryWrapper<PostImageEntity>().eq("post_id", postId));
        return Result.ok("删除成功");
    }

}